package com.itfreer.bpm.workflow.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.flow.findwork.para.FindWorkInfo;
import com.itfreer.bpm.flow.findwork.para.MultiTask;
import com.itfreer.bpm.flow.findwork.para.UniversalTask;
import com.itfreer.bpm.history.dao.IBpmHistoryDao;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.option.para.TaskType;
import com.itfreer.bpm.power.para.UserInfo;

/**
 * 审批审核意见构建工具
 * @author gj
 *
 */
@Component
public class BpmApprovalHistoryUtil {
	
	@Autowired
	IBpmHistoryDao historydao;
	/**
	 * 设置项目历史审批信息
	 * @param info
	 * @param bpmKey
	 * @param hentity
	 */
	public void setTaskHisoty(FindWorkInfo info,String sexeid,UserInfo user) {
		HashMap<String,Object> history_where=new HashMap<String,Object>();
		history_where.put("sexeid:=", sexeid);
		history_where.put("historystatus:!=", "2");
		HashMap<String,Integer> history_order=new HashMap<String,Integer>();
		history_order.put("fromtime", 0);
		List<BpmHistoryEntity>lst= historydao.getEntitys(null, history_where, history_order, 10000, 1);
		if(lst==null || lst.size()<1) return;
		String overTask="";
		
		for(int k=0;k<lst.size();k++) {
			BpmHistoryEntity entity=lst.get(k);
			//构建数据是否已经处理过历史信息表
			if(overTask.contains(","+entity.gettaskdefid()+",")) continue;
			overTask=overTask+","+entity.gettaskdefid()+",";
			
			String tasktype=entity.getTasktype();
			//普通节点，一个节点就只有一个环节审批
			if(TaskType.CommonTask.toString().equals(tasktype)) {
				//普通节点处理过程，收集节点id一样的环节，即可
				UniversalTask universalTask=new UniversalTask();
				universalTask.setTaskdefinedid(entity.gettaskdefid());
				universalTask.setTaskName(entity.gettaskdefname());
				for(int j=k;j<lst.size();j++) {
					
					BpmHistoryEntity jentity=lst.get(j);
					
					if(entity.gettaskdefid().equals(jentity.gettaskdefid())) {
						String historystatus=jentity.gethistorystatus();
						String tab=jentity.getTasktab();
						//现状数据保存为当前编辑数据
						if("21".equals(historystatus) || "1".equals(historystatus)) {
							String toptuser=jentity.getToptassigneese();
							if((toptuser!=null && toptuser.contains(","+user.getUserId()+","))
								|| user.isSuperAdmin()) {
								jentity.setSread("1");
								universalTask.setUserEdit(true);
								universalTask.setEditRow(jentity);
								info.setCtab(tab);
							}
							
						}else {
							universalTask.getHistory().add(jentity);
							this.addTabs(info, tab);
						}
						
						
					}
					
				}
				info.getUniversalTask().add(universalTask);
			}else {
				//单节点会签组合方式为节点id和待办项人的id
				String overZhtask="";
				
				MultiTask multitask=new MultiTask();
				multitask.setTaskdefinedid(entity.gettaskdefid());
				multitask.setTaskName(entity.gettaskdefname());
				
				//收集逻辑，首先判断节点id是否一致，在一致的情况下，降节点id和单节点显示的代办人组合成组成键，构建节点
				for(int j=k;j<lst.size();j++) {			
					BpmHistoryEntity jentity=lst.get(j);
					
					//遍历会审节点
					if(entity.gettaskdefid().equals(jentity.gettaskdefid())) {
						
						//组合节点id和用户id构建记录
						String j_task=","+jentity.gettaskdefid()+"-"+jentity.getToptassigneese()+",";
						
						if(overZhtask.contains(j_task)) continue;
						overZhtask=overZhtask+","+j_task;
						
						UniversalTask universalTask=new UniversalTask();
						universalTask.setTaskdefinedid(j_task);
						universalTask.setTaskName(entity.gettaskdefname());
						
						for(int i=j;i<lst.size();i++) {
							BpmHistoryEntity ientity=lst.get(i);
							String historystatus=ientity.gethistorystatus();
							String tab=ientity.getTasktab();
							
							//这个地方缺少一层状态码
							String i_task=","+ientity.gettaskdefid()+"-"+ientity.getToptassigneese()+",";
							if(i_task.equals(j_task)) {
								//现状数据保存为当前编辑数据
								if("21".equals(historystatus) || "1".equals(historystatus)) {
									String toptuser=ientity.getToptassigneese();
									//判断当前编辑数据是否具有相关权限
									if((toptuser!=null && toptuser.contains(","+user.getUserId()+","))
											|| user.isSuperAdmin()) {
										ientity.setSread("1");
										universalTask.setUserEdit(true);
										universalTask.setEditRow(ientity);
										info.setCtab(tab);
									}
								
								}else {
									universalTask.getHistory().add(ientity);
									this.addTabs(info, tab);
								}
							}
						}
						multitask.getSingleTask().add(universalTask);
					}
				}
				info.getMultiTask().add(multitask);
			}
		}
	}
	
	/**
	 * 添加过程表单问题
	 * @param info
	 * @param tab
	 */
	private void addTabs(FindWorkInfo info,String tab) {
		if(info.getTabs()==null) {
			info.setTabs(new ArrayList<String>());
		}
		if(tab!=null && !"".equals(tab)) {
			if(!info.getTabs().contains(tab)) {
				info.getTabs().add(tab);
			}
		}
	}
	
	/**
	 * 列表转换成objectlist
	 * @param lst
	 * @return
	 */
	private List<Object> convertHistoryToObject(List<BpmHistoryEntity>lst){
		if(lst==null || lst.size()<1)return null;
		else {
			 List<Object> nlst=new ArrayList<Object>();
			 for(BpmHistoryEntity entity:lst) {
				 nlst.add(entity);
			 }
			 return nlst;
		}
	}
}
